<!-- Web Upload Data Delete PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/06/08 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. 增加 FTP 上 Delete File 功能, 及是否要 delete 勾选
		3. 可删除 MySQL 上, 不同站别, 但相同版本及描述
	Reb 1.08 at 2015/12/08 by Max Yin,
		1. Web SMT upload 增加-回流焊的选项。(eric_liang)
		2. Web SMT upload/download, 由填入栏位改为下拉选单。(eric_liang)
		3. Web Upload/Downlaod 新增加 ESD 下拉选单。(jianwei_wang)
		4. 移除 ftp IP 的栏位输入, 由 Web 程式内定 FTP 路径。(Lucas)
		5. 修正重复上传, 却只有版本不对, 导致无法上传问题。(mingshen_feng)
		6. 修正若上传者, 若没有上传者名称, 会要求重新登入讯息。
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta charset="UTF-8">
	</head>
	<body>
		<center>
			<?php
				require_once('class_package/common.php');
				$tmp_pn			= "";
				$tmp_wsid		= "";
				$tmp_tpver		= "";
				$tmp_work_order	= "";	
				$tmp_line		= "";
				$tmp_panel		= "";
				$tmp_ftpip		= "";
				
				if ( ! @isset($_GET[pn]) || ! @isset($_GET[wsid]) || ! @isset($_GET[tpver]) || ! @isset($_GET[work_order]) || ! @isset($_GET[line]) || ! @isset($_GET[panel]) || ! @isset($_GET[ftpip]) ) {
					echo "<script language=javascript>alert('无此信息!');history.go(-1);</script>";
				}	
				$tmp_pn			= @$_GET[pn];
				$tmp_wsid		= @$_GET[wsid];
				$tmp_tpver		= @$_GET[tpver];
				$tmp_work_order	= @$_GET[work_order];	
				$tmp_line		= @$_GET[line];
				$tmp_panel		= @$_GET[panel];
				$tmp_ftpip		= @$_GET[ftpip];
				
				// To check FTP files exist...
				if ( $tmp_ftpip == NULL ) {
					echo "<script>alert('在资料库获取不到相关该笔删除资料的FTP文件IP, 请查询后再删除...');history.go(-1);</script>";
					exit();
				}
				
				// delete ftp old file on ftp side...
				if ( is_file($tmp_ftpip) ) unlink($tmp_ftpip);
				
				// To delete the account by user name from MySQL ...
				$result = download_delete($tmp_pn,$tmp_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel);				
				if ( !$result ) {
					echo "<script>alert('该程式栏位, 删除失败! 请联系程式开发人员, 并查明原因...');history.go(-1);</script>";
					exit();
				}
				echo "<script>alert('该程式栏位, 已删除成功!');history.go(-2);</script>";
				exit();
				
				if (0) {
					setcookie("search_arr[$tmp_pn][$tmp_wsid]",0);
					
					// $tmp_pn = CRL615766000A,CRL615766007A,...
					$tmp_pn				= strtoupper(@$_POST['webul_pn']);
					$tmp_wsid			= strtoupper(@$_POST['webul_wsid']);
					$tmp_tpver			= strtoupper(@$_POST['webul_mfg_ver']);
					$tmp_work_order		= strtoupper(@$_POST['webul_work_order']);
					$tmp_descript		= @$_POST['webul_descript'];
					$tmp_by				= @$_COOKIE['login_user'];
					if ( $tmp_by == '' ) {
						echo "<script>alert('登入帐号不能为空值, 请回到 http://webdl.joytech.com.cn/webul_login.html 使用者登入网页, 重新登入!');history.go(-1);</script>";
						exit();
					}				
					if ( strlen($tmp_wsid) == 0 || strlen($tmp_wsid) > 60 ) {
						echo "<script>alert('输入的-WSID(站別)或其他栏位中, 超过30个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strlen($tmp_tpver) == 0 || strlen($tmp_tpver) > 30 ) {
						echo "<script>alert('输入的-TPVER(程式版本)或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($tmp_tpver,' ') > -1 ) {
						echo "<script>alert('输入的TPVER(程式版本)的栏位中, 不能为\' \'空白,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
						if ( $tmp_pn != 'N/A' ) {
							echo "<script>alert('输入的栏位中,Part Number(局部料号)应为\'N/A\'字串, 请重新输入!!');history.go(-1);</script>";
							exit();
						}
					}
					if ( strpos($tmp_wsid,'SMT') > -1 || strpos($tmp_wsid,'ICT') > -1 || strpos($tmp_wsid,'SCT') > -1 || strpos($tmp_wsid,'H/P') > -1 || strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
						if ( $tmp_work_order != 'N/A' ) {
							echo "<script>alert('输入的-Work Order(工單)应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					} else {
						if ( $tmp_work_order == 'N/A' ) {
							echo "<script>alert('输入的-Work Order(工單)不应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					}
					
					// To search all table ...
					$db_select      = Sql_Download::sql_db;
					$db_table       = Sql_Download::sql_table;
					$table_name1    = Sql_Download::table_name1;	// 'webdl_pn';
					$table_name2    = Sql_Download::table_name2;	// 'webdl_wsid';
					$table_name3    = Sql_Download::table_name3;	// 'webdl_tpver';
					$table_name4    = Sql_Download::table_name4;	// 'webdl_work_order';
					$table_name8	= Sql_Download::table_name8;	// 'webdl_create_date';
					$table_name9	= Sql_Download::table_name9;	// 'webdl_creator';
					$table_name10   = Sql_Download::table_name10;	// 'webdl_description';
					
					// To delete over 2 years columns..
					$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
					if ( mysqli_connect_errno($conn) ) { 
						echo "<script>alert('MySQL资料库连接错误, 请联系统开发人员!!');history.go(-1);</script>";
						exit();
					}
					
					mysqli_set_charset($conn,'set names utf8');
					$sqli_cmd = "SELECT * FROM `$db_table` WHERE `$table_name9`='$tmp_by' ORDER BY (`$table_name8`) DESC";
					$res = mysqli_query($conn,$sqli_cmd);	// 透过 execute_query 对象实例完成查询
					if ( !$res ) { 
						mysqli_close($conn);				// 关闭资料库
						echo "<script>alert('MySQL资料库连接错误, 请联系统开发人员!!');history.go(-1);</script>";
						exit();
					}
					
					$row = mysqli_num_rows($res);		// 返回结果集中栏位的数量。
					if ( $row > 0 ) {														
						while ( $getArrays = mysqli_fetch_array($res,MYSQLI_ASSOC) ) {					
							$put_pn          = $getArrays['webdl_pn'];
							$put_wsid        = $getArrays['webdl_wsid'];
							$put_tpver       = $getArrays['webdl_tpver'];
							$put_work_order  = $getArrays['webdl_work_order'];
							$put_line        = $getArrays['webdl_line'];
							$put_panel       = $getArrays['webdl_panel'];
							$put_create_date = $getArrays['webdl_create_date'];
							$put_creator     = $getArrays['webdl_creator'];
							$put_description = $getArrays['webdl_description'];
							$put_ftpip       = $getArrays['webdl_ftpip'];
							$d1=strtotime(date('Y-m-d G:i:s')); 
							$d2=strtotime($put_create_date);
							$d3=$d1-$d2;
							$Days=round(($d1-$d2)/3600/24);					
							/* 1090  3*/	
							/* 730  2*/	
							/* 1460  4*/	
				
							if ( $Days > 730 ) {
								if ( is_file($put_ftpip) ) unlink($put_ftpip);
								download_delete($put_pn,$put_wsid,$put_tpver,$put_work_order,$put_line,$put_panel);
							}
						}
					}
					mysqli_close($conn);	// 关闭资料库
					
					// To search new data columns...
					unset($get_pn_array);
					if ( strpos($tmp_pn,',') > -1 )
						$get_pn_array = preg_split("/[\s,]+/",$tmp_pn);
					else
						$get_pn_array[] = $tmp_pn;
					
					$PN_chkCnt		= 0;
					$show_num		= 0;
					$getPN_ArrCnt	= count($get_pn_array);
					foreach ( $get_pn_array as $each_pn ) {
						$err_chk        = '';
						unset($get_wsid_array);
						$PN_chkCnt++;
						
						// To check user name & password limit-1 ...
						if ( strlen($each_pn) == 0 || strlen($each_pn) > 13 ) {
							echo "<script>alert('输入的-Part Number(局部料号), 逗号\',\'后不能为空值或超过13个字符, 请重新输入!!');history.go(-1);</script>";
							exit();
						}
						
						if ( strpos($tmp_wsid,',') > -1 )
							$get_wsid_array = preg_split("/[\s,]+/",$tmp_wsid);
						else
							$get_wsid_array[] = $tmp_wsid;
						
						$WSID_chkCnt  = 0;
						$getArrCnt    = count($get_wsid_array);
						foreach ( $get_wsid_array as $each_wsid ) {
							$source_cookie	= @$_COOKIE['search_arr'][$each_pn][$each_wsid];
							if ( $source_cookie == 0 ) continue;
							$conn		= '';
							$sql_cmd	= '';
							$rows		= 0;
							$result		= '';
							$WSID_chkCnt++;
							
							// Check connection
							$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
							if ( mysqli_connect_errno($conn) ) { 
								$err_chk = 'SQL_Conn_Fail';
								break;
							}
							mysqli_set_charset($conn,'set names utf8');
							
							// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
							// SELECT *  FROM `download_data` WHERE `pn` = 'CRL615766000A' AND `wsid` LIKE 'F/T-B%' AND `tpver` = '0X1.16'
							if ( $each_wsid == 'ROUTER' || $each_wsid == 'WS-PROFILE' || $each_wsid == 'DOC' || $each_wsid == 'ESD' ) {
								$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$each_wsid' AND `$table_name3` = '$tmp_tpver' AND `$table_name10` LIKE '$tmp_descript%'";
							} elseif ( strpos($each_wsid,'SMT') > -1 || strpos($each_wsid,'ICT') > -1 || strpos($each_wsid,'SCT') > -1 ) {
								$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$each_pn' AND `$table_name2` LIKE '$each_wsid%' AND `$table_name3`='$tmp_tpver' AND `$table_name10` LIKE '$tmp_descript%'";
							} else {
								$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$each_pn' AND `$table_name2`='$each_wsid' AND `$table_name3`='$tmp_tpver' AND `$table_name4`='$tmp_work_order' AND `$table_name10` LIKE '$tmp_descript%'";
							}
							$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
							if ( !$result ) { 
								mysqli_close($conn);				// 关闭资料库
								$err_chk = 'SQL_Search_Fail';
								break;
							}
							
							$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
							if ( $rows == 0 ) { 
								$err_chk = 'WSID_None';
								break;
							}
							
							if ( $PN_chkCnt == 1 && $WSID_chkCnt == 1 ) {
								echo '<br>
								<table border="2" width="75%">
									<tr bgcolor="#70DB93">
										<th>No.</th>
										<th>工單</th>
										<th>Part Number</th>
										<th>WSID</th>
										<th>TPVER</th>
										<th>线别</th>
										<th>制程别</th>
										<th>Create Date/Time</th>
										<th>Create By</th>
										<th>Description</th>
										<th>Download</th>
										<th>Delete</th>
										<th>Update</th>
									</tr>';
							}
							
							while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
								$show_num++;
								$regex = '/\/COMMON\//i';
								$put_pn          = $getArray['webdl_pn'];
								$put_wsid        = $getArray['webdl_wsid'];
								$put_tpver       = $getArray['webdl_tpver'];
								$put_work_order  = $getArray['webdl_work_order'];
								$put_line        = $getArray['webdl_line'];
								$put_panel       = $getArray['webdl_panel'];
								$put_create_date = $getArray['webdl_create_date'];
								$put_creator     = $getArray['webdl_creator'];
								$put_description = $getArray['webdl_description'];
								$put_ftpip       = $getArray['webdl_ftpip'];
								$webul_update    = "webul_download_update.html?tpver=$put_tpver,wsid=$put_wsid";
								$webul_delete	 = "";
								if ( preg_match($regex, $put_ftpip) )
									$webul_delete = "webul_download_delete.html?pn=$put_pn,tpver=$put_tpver,wsid=$put_wsid";																					
								else
									$webul_delete = "webul_download_delete_b.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip";
								
								echo "
									<tr>
										<th>$show_num</th>
										<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
										<th>$put_pn</th>
										<th>$put_wsid</th>
										<th>$put_tpver</th>
										<th>$put_line</th>
										<th>$put_panel</th>
										<th>$put_create_date</th>
										<th>$put_creator</th>
										<th>$put_description</th>
										<th><a href=$put_ftpip>下 载</a></th>
										<th><a href=$webul_delete>删 除</a></th>
										<th><a href=$webul_update>修 改</a></th>
									</tr>";
							}
							if ( $PN_chkCnt == $getPN_ArrCnt && $WSID_chkCnt == $getArrCnt ) echo '</table><br>';
							mysqli_free_result($result);			// 释放 MySQL $result
							mysqli_close($conn);					// 关闭资料库
						}
						
						// String strTemp="历史答题报告：\\n"+"您答对了"+RdData.getCcnt()+"道题\\n"+"您有"+RdData.getBcnt()+"道题没答\\n"+"您答错"+RdData.getWcnt()+"题\\n"; 
						// out.print("<script> alert('"+strTemp+"');window.location='index.jsp'</script>"); 
						if ( $err_chk == 'SQL_Conn_Fail' ) {
							echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
							exit();
						} elseif ( $err_chk == 'SQL_Search_Fail' ) {
							echo "<script>alert('MySql资料库搜寻失败,请跟开发人员查明原因...');history.go(-1);</script>";
							exit();
						} elseif ( $err_chk == 'WSID_None' ) {
							echo "<script>alert('WSID(站別) 栏位中, 有某些站位查询不到, 请重新建立...');history.go(-1);</script>";
							exit();
						}
					}
				}
			?>
		</center>
	</body>
</html>

